package com.baseframe.sys.controller;

import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baseframe.common.annotation.SysLog;
import com.baseframe.common.annotation.VisitLog;
import com.baseframe.common.utils.R;
import com.baseframe.sys.entity.FrameOu;
import com.baseframe.sys.service.IFrameOuService;

/**
 * 部门管理
 * 
 * @author mzl
 */
@RestController
@RequestMapping("/sys/dept")
public class FrameDeptController extends AbstractController {
	@Autowired
	private IFrameOuService ouService;

	/**
	 * 列表
	 */
	@VisitLog("部门列表")
	@RequestMapping("/list")
	public List<FrameOu> list() {
		List<FrameOu> deptList = ouService.selectList();
		// 插入name
		for (FrameOu ou : deptList) {
			FrameOu parent = ouService.selectById(ou.getParentouguid());
			if (parent != null) {
				ou.setParentName(parent.getOuname());
			} else {
				ou.setParentName("根");
			}

		}
		return deptList;
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@VisitLog("部门列表")
	@RequestMapping("/select")
	public R select() {
		List<FrameOu> deptList = ouService.selectList();
		// 插入name
		for (FrameOu ou : deptList) {
			FrameOu parent = ouService.selectById(ou.getParentouguid());
			if (parent != null) {
				ou.setParentName(parent.getOuname());
			} else {
				ou.setParentName("一级部门");
			}

		}
		/*
		 * //添加一级部门 if(getUserId() == Constant.SUPER_ADMIN){ FrameOu root = new
		 * FrameOu(); root.setOuguid("0"); root.setOuname("一级部门");
		 * root.setParentouguid("-1"); root.setOpen(true); deptList.add(root); }
		 */

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	/*@RequestMapping("/info")
	@RequiresPermissions("sys:dept:list")
	public R info() {
		String deptId = "0";
		if (getUserId() != Constant.SUPER_ADMIN) {
			
			 * List<FrameOu> deptList = ouService.selectList(); String parentId
			 * = null; for(FrameOu sysDeptEntity : deptList){ if(parentId ==
			 * null){ parentId = sysDeptEntity.getParentouguid(); continue; }
			 * 
			 * if(parentId > sysDeptEntity.getParentouguid()){ parentId =
			 * sysDeptEntity.getParentId(); } } deptId = parentId;
			 
		}

		return R.ok().put("deptId", deptId);
	}*/

	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	public R info(@PathVariable("deptId") String deptId) {
		FrameOu dept = ouService.selectById(deptId);
		FrameOu parent = ouService.selectById(dept.getParentouguid());
		if (parent != null) {
			dept.setParentName(parent.getOuname());
		} else {
			dept.setParentName("一级部门");
		}
		return R.ok().put("dept", dept);
	}

	/**
	 * 保存
	 */
	@SysLog(value="保存部门", type="新增")
	@RequestMapping("/save")
	public R save(@RequestBody FrameOu dept) {
		// 数据校验
		// verifyForm(dept);
		dept.setOuguid(UUID.randomUUID().toString());
		ouService.insert(dept);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog(value="修改部门", type="修改")
	@RequestMapping("/update")
	public R update(@RequestBody FrameOu dept) {
		ouService.update(dept);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@SysLog(value="删除部门", type="删除")
	@RequestMapping("/delete")
	public R delete(String deptId) {
		// 判断是否有子部门
		List<FrameOu> deptList = ouService.queryListParentId(deptId);
		if (deptList.size() > 0) {
			return R.error("请先删除子部门");
		}

		ouService.delete(deptId);

		return R.ok();
	}

}
